#include "widget.h"
#include "ui_widget.h"
#include <QShortcut>
#include <QRect>
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 使用快捷键“+”，“-”操作
    ui->horizontalSlider->setMinimum(500);
    ui->horizontalSlider->setMaximum(2000);
    ui->horizontalSlider->setValue(800);
    ui->horizontalSlider->setSingleStep(100);
    QShortcut* Add = new QShortcut(this);
    Add->setKey(QKeySequence("+"));
    int value = ui->horizontalSlider->value() + 100;
    connect(Add, &QShortcut::activated, [this]() {
            int value = ui->horizontalSlider->value() + 100;
            if (value <= ui->horizontalSlider->maximum()) {
                AddH(value);
                ui->horizontalSlider->setValue(value);
            }
        });

    QShortcut* Sub = new QShortcut(this);
    Sub->setKey(QKeySequence("-"));
    connect(Sub, &QShortcut::activated, [this]() {
            int value = ui->horizontalSlider->value() - 100;
            if (value >= ui->horizontalSlider->minimum()) {
                SubH(value);
                ui->horizontalSlider->setValue(value);
            }
        });
}

Widget::~Widget()
{
    delete ui;
}

void Widget::AddH(int val)
{
//    int val = ui->horizontalSlider->value();
//    ui->horizontalSlider->setValue(val+1);

    QRect rect = this->geometry();
//    int val = ui->horizontalSlider->value()+100;
    qDebug() << val;
    this->setGeometry(rect.x(),rect.y(),val,rect.height());
}
void Widget::SubH(int val)
{
//    int val = ui->horizontalSlider->value();
//    ui->horizontalSlider->setValue(val-1);

    QRect rect = this->geometry();
//    int val = ui->horizontalSlider->value()-100;
    qDebug() << val;
    this->setGeometry(rect.x(),rect.y(),val,rect.height());
}

void Widget::on_horizontalSlider_valueChanged(int value)
{
    ui->label->setText(QString("当前的值为：") + QString::number(value));
}
